package com.cjst.controller.base;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjst.pojo.base.ShortTemplate;
import com.cjst.pojo.part.PartType;
import com.cjst.pojo.part.PtypeRelevance;
import com.cjst.pojo.sys.SysRelation;
import com.cjst.sdp.util.ZTreeNode;
import com.cjst.service.base.ShortTemplateService;
import com.cjst.service.sys.SysRelationService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.code.PageUtil;
import com.cjst.util.json.ResultObj;

@Controller
@RequestMapping("/base/shortTemplate")
public class ShortTemplateController extends BaseController{
	@Autowired
	private ShortTemplateService shortTemplateService;
	
	@Autowired
	private SysRelationService sysRelationService;
	
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String toDept() {
	    return "base/shortTemplate/list";
	}
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public Object list() {
	    PageUtil<ShortTemplate> pageUtil = shortTemplateService.listPageCount(null);
	    return super.packForBT(pageUtil);
	}
	@RequestMapping(value = "/sx", method = RequestMethod.POST)
	@ResponseBody
	public ResultObj sx() {
		shortTemplateService.sx();
	    return new ResultObj(0, "刷新成功", true);
	}
	
	  /**
     * 短信权限数据
     * @param roleId
     * @param model
     * @return
     * @throws Exception
     */					 
    @RequestMapping(value = "/toSmsAssign/{roleId}")
    @RequiresPermissions("parts_relevance")
    public String roleSmsAssign(@PathVariable("roleId") String roleId, Model model) throws Exception {
        if (StringUtils.isEmpty(roleId)) {
            throw new Exception("参数传递为空");
        }
        model.addAttribute("roleId", roleId);
        
        return "system/role/role_sms_assign";
    }
    
    /**
     * 关联
     * @param roleId
     * @return
     * @throws Exception
     */   
    @RequestMapping(value = "/smsTreeListByRoleId/{roleId}")
    @ResponseBody
    public List<ZTreeNode> smsTreeListByRoleId(@PathVariable String roleId) throws Exception {
        if (StringUtils.isEmpty(roleId)) {
            throw new Exception("参数传递为空，查询失败");
        }
        WherePrams wherePrams = Method.where("roleid", C.EQ, roleId);
        wherePrams.and("clzz",C.EQ,2);
        List<SysRelation> relations = sysRelationService.list(wherePrams);
        //获取拥有的菜单id
        List<String> menuIds = new ArrayList<>();
        for (SysRelation relation : relations) {
            menuIds.add(relation.getMenuid());
        }
        
        //WherePrams partTypePrams = Method.where("delFlag", C.EQ, "0");
        //查询整个菜单列表
        List<ShortTemplate> shortTemplates = shortTemplateService.list(null);

        List<ZTreeNode> nodes = new ArrayList<>();
        //遍历menus
        for (ShortTemplate shortTemplate : shortTemplates) {
            ZTreeNode node = new ZTreeNode();
            node.setId(shortTemplate.getId());
            node.setName(shortTemplate.getTemplateCode()+":"+shortTemplate.getTemplateName());
            //判断menusId是否为空,查询menus哪些是在menusId中，并给出标记
            if (menuIds.size() > 0 && menuIds.contains(shortTemplate.getId())) {
                node.setChecked(true);
            } else {
                node.setChecked(false);
            }
            nodes.add(node);
        }
        return nodes;
    }

    /**
     * 配置权限
     */
    @RequestMapping("/setSmsRelation")
    @ResponseBody
    @RequiresPermissions("role_sms_assign")
    public Object setSmsRelation(@RequestParam("roleId") String roleId,
        @RequestParam("ids") String ids) throws Exception {
        if (StringUtils.isEmpty(roleId)) {
            return new ResultObj(1, "参数传递为空，权限配置失败", null);
        }
        
        sysRelationService.setSmsRelation(roleId, ids);
        return new ResultObj(0, "成功", null);
    }
    
    
    
	
	
}
